package com.cw.config;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.pojo.TbUser;
import com.cw.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class LoginRealm extends AuthorizingRealm {

    @Reference
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

        TbUser tbUser = userService.selectUserByUsername(token.getUsername());

        if (tbUser == null) {

            return null;
        }

        return new SimpleAuthenticationInfo(tbUser, tbUser.getPassword(), ByteSource.Util.bytes(tbUser.getSalt()), tbUser.getUsername());
    }
}
